#include "StdAfx.h"
#include "DrawLabel.h"
namespace UILib
{

    CDrawLabel::CDrawLabel(void) : CDrawView()\
        , uAlign_(DT_LEFT)
        , color_(RGB(255,255,255))
        , bGDIPlushEnable_(false)
    {
    }

    CDrawLabel::CDrawLabel( const PUIRect pRect, const UINT uID /*= -1*/, ViewListener* pListener /*= NULL */ ) : CDrawView(pRect, uID, pListener)\
        , uAlign_(DT_LEFT)
        , color_(RGB(255,255,255))
        , bGDIPlushEnable_(false)
    {

    }
    CDrawLabel::~CDrawLabel(void)
    {
    }

    void CDrawLabel::DrawElement( const HDC hDC, const RECT &rcPaint )
    {
        CDrawView::bNeedInvalidate_ = false;
        if ( !IsVisible() )
        {
            return;
        }
        RECT rcEmem = { 0 };
        CDrawView::GetRECT(rcEmem);

        if ( bGDIPlushEnable_ && !wstrText_.empty() )
        {	
            FontFamily fontFamily(lf_.lfFaceName);
            StringFormat strformat;
            if ( ( uAlign_& DT_CENTER ) == DT_CENTER )
            {
                strformat.SetAlignment( StringAlignmentCenter  );
            }
            else if ( ( uAlign_& DT_RIGHT ) == DT_RIGHT )
            {
                strformat.SetAlignment( StringAlignmentFar  );
            }
            else if ( (uAlign_ &  DT_LEFT) == DT_LEFT )
            {
                strformat.SetAlignment( StringAlignmentNear  );
            }
            int nHeight = -lf_.lfHeight;

            FontStyle fontStyle = FontStyleRegular;
            if ( lf_.lfWeight > 400 )
            {
                fontStyle =	 FontStyleBold;
            }

            Font font(&fontFamily, (REAL)nHeight, fontStyle, UnitPixel);
            Color cl(GetRValue(color_),GetGValue(color_),GetBValue(color_));
            SolidBrush sBrush(cl);

            Graphics graph(hDC);
            RectF RcDest((REAL)rcEmem.left, (REAL)rcEmem.top, (REAL)(rcEmem.right - rcEmem.left), (REAL)(rcEmem.bottom - rcEmem.top) );
            graph.DrawString(wstrText_.c_str(), wstrText_.length(), &font, RcDest, &strformat, &sBrush);
        }
        else if (!wstrText_.empty())
        {
            RECT rcEdit = {0};
            memcpy(&rcEdit, &rcEmem, sizeof(RECT));
            HFONT hfont = ::CreateFontIndirect(&lf_);
            HFONT holdFont = (HFONT)::SelectObject(hDC, hfont);
            COLORREF oldColor = ::SetTextColor(hDC, color_ );
            int oldbkgMode = ::SetBkMode(hDC, TRANSPARENT);
            ::DrawText(hDC, wstrText_.c_str(), -1, &rcEdit, DT_VCENTER |DT_NOPREFIX | DT_EDITCONTROL | uAlign_ );
            ::SetBkMode(hDC, oldbkgMode);
            ::SelectObject(hDC, holdFont);
            ::SetTextColor(hDC, oldColor);
            ::DeleteObject(hfont);
        }
    }

    void CDrawLabel::SetText( wstring wszText, const LOGFONT lf, const COLORREF color /*= 0x000000 */ )
    {
        wstrText_ = wszText;
        memcpy(&lf_, &lf, sizeof(LOGFONT));
        color_ = color;
        CDrawView::ForceInvalidate();
    }

    void CDrawLabel::SetText( wstring wszText )
    {
        wstrText_ = wszText;
        CDrawView::ForceInvalidate();
    }

    void CDrawLabel::SetGDIPlusEnable( const bool bEnable )
    {
        bGDIPlushEnable_ = bEnable;
    }

}